<template>
  <div class="content">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="所属部门">
          <el-select v-model="query.department_id">
            <el-option label="所有部门" :value="0"></el-option>
            <el-option
              v-for="item in departmentOption"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属岗位">
          <el-select v-model="query.station_id">
            <el-option label="所有岗位" :value="0"></el-option>
            <el-option
              v-for="item in stationOption"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名|电话">
          <el-input
            v-model="query.keyword"
            placeholder="请输入员工姓名或联系电话"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getTableInfo"
            >查询</el-button
          >
          <el-button
            type="primary"
            icon="Plus"
            @click="handleLeave()"
            v-auth="'/company/resign/add'"
            >办理离职</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <el-table
      :data="tableData"
      row-key="user_id"
      default-expand-all
      height="90%"
      stripe
    >
      <el-table-column type="index" label="#" sortable />
      <el-table-column prop="user_id" label="会员ID" width="80px" />
      <el-table-column prop="user.real_name" label="姓名" />
      <el-table-column prop="job_id" label="工号" />
      <el-table-column prop="department.title" label="部门" />
      <el-table-column prop="station.title" label="岗位" />
      <el-table-column label="职位">
        <template #default="scope">
          {{
            scope.row.type == 0 ? "员工" : scope.row.type == 1 ? "主管" : "经理"
          }}
        </template>
      </el-table-column>
      <el-table-column prop="user.mobile" label="手机号" />
      <el-table-column prop="join_time" label="入职日期" width="120px" />
      <el-table-column prop="status_text" label="状态" width="60px" />
      <el-table-column prop="regular_time" label="转正日期" width="120px" />
      <el-table-column prop="leave_time" label="离职日期" width="120px" />
      <el-table-column label="操作" width="240px" fixed="right">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleInfo(scope.row.user_id)"
            v-auth="'staff/edit'"
          >
            离职信息
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider></el-divider>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <LeaveDrawer ref="leaveRef" @getTable="getTableInfo" />
    <LeaveInfoDrawer ref="leaveInfoRef"/>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import { getDepartmentOptionsApi } from "@/api/company/department";
import { ElMessage, ElMessageBox } from "element-plus";
import { delApi, getListAPi } from "@/api/company/staff";
import { getListAPi as getStationApi } from "@/api/company/station";
import LeaveInfoDrawer from "./component/leaveInfoDrawer.vue";
import LeaveDrawer from "./component/leaveDrawer.vue";
const departmentOption = ref<any[]>([]);
const stationOption = ref<any[]>([]);
const query = reactive({
  department_id: 0,
  station_id: 0,
  keyword: "",
  residence_permit: -1,
  is_hostel: -1,
  status: 0,
});
const leaveRef = ref<any>(null);
const leaveInfoRef = ref<any>(null);
const total = ref(0);
watch(
  query,
  () => {
    getTableInfo();
  },
  { deep: true }
);
watch(
  () => query.department_id,
  (newVal) => {
    query.station_id = 0;
    if (newVal) {
      getStationOptions();
    } else {
      stationOption.value = [];
    }
  }
);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
onMounted(() => {
  getDepartmentOptionsApi().then((res) => {
    res.data.forEach((el: any) => {
      departmentOption.value.push({
        id: el.id,
        title: el.title,
      });
      if (el.children && el.children.length) {
        el.children.forEach((el: any) => {
          departmentOption.value.push({
            id: el.id,
            title: el.title,
          });
        });
      }
    });
  });
  getTableInfo();
});
const getStationOptions = () => {
  getStationApi({
    department_id: query.department_id,
  }).then((res) => {
    stationOption.value = res.data;
  });
};
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};

function getTableInfo() {
  getListAPi({
    page: curPage.value,
    page_size: pageSize.value,
    ...query,
  }).then((res) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function handleLeave() {
  leaveRef.value.handleOpen();
}

function handleInfo(id: number) {
  leaveInfoRef.value.handleOpen(id);
}
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
